layui.use(['layer', 'form'], function () {
    var layer = layui.layer;
    var $ = layui.jquery;
    var form = layui.form;

    form.verify({
        pass: [
            /^[\S]{6,12}$/
            , '密码必须6到12位，且不能出现空格'
        ],
        servercap: function (value, item) {
            if (servercap == "" || servercap == undefined) {
                return '验证码已过期 请重新请求';
            }
        }
    });

    form.on('submit(signup)', function (data) {
        $.ajax({
            url: 'http://169.254.202.137:8080/SignUpAccount?',
            data: data.field,
            type: 'get',
            dataType: 'json',
            success: function (res) {
                if (res.code == 200) {
                    // Use layui.layer.msg instead of layer.msg
                    layui.layer.msg('注册成功', { icon: 1, time: 1500 }, function () {
                        // 使用window.location.replace来替换当前页面，不会触发表单事件的提交
                        window.location.replace("Login.html")
                    });
                } else if (res.code == 401) {
                    layui.layer.msg('验证码错误!', { icon: 2, time: 1500 }, function () {
                        vm.username = '';
                        vm.email = '';
                        vm.password = '';
                        vm.cap = '';
                        vm.servercap = '';
                    });
                }
            }
        })
        return false;
    });

    form.on('submit(signin)', function (data) {
        $.ajax({
            url: 'http://169.254.202.137:8080/SignInAccount?',
            data: data.field,
            type: 'post',
            dataType: 'json',
            success: function (res) {
                if (res.code == 200) {
                    // Use layui.layer.msg instead of layer.msg
                    layui.layer.msg('登录成功', { icon: 1, time: 1500 }, function () {
                        sessionStorage.setItem("user", JSON.stringify(res.data))
                        // 使用window.location.replace来替换当前页面，不会触发表单事件的提交
                        window.location.replace("Home.html")
                    });
                } else if (res.code == 401) {
                    layui.layer.msg(res.msg, { icon: 2, time: 1500 }, function () {

                    });
                } else if (res.code == 444) {
                    layui.layer.msg(res.msg, { icon: 2, time: 1500 }, function () {

                    });
                }
            }
        })
        return false;
    });
})